package com.maven.logging;

import org.apache.commons.lang3.time.StopWatch;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: EAD-MASTER
 * Date: 5/15/13
 * Time: 6:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class LoggingBenchmarkTest2 extends AbstractLoggingBenchmark {

	StopWatch stopWatch = new StopWatch();


	@Test
	public void runLog4jWithCheck1Test() {
		org.apache.log4j.Logger log4j = Logger.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (log4j.isDebugEnabled()) {
				log4jNormalString(log4j);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		log4j.info("runLog4jTest(): Time=" + convert + "ms");
	}

	@Test
	public void runLog4jWithCheck2Test() {
		org.apache.log4j.Logger log4j = Logger.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (log4j.isDebugEnabled()) {

				log4jWithStringBuilder(log4j);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		log4j.info("runLog4jTest(): Time=" + convert + "ms");
	}

	@Test
	public void runLog4jWithCheck3Test() {
		org.apache.log4j.Logger log4j = Logger.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (log4j.isDebugEnabled()) {

				log4jWithStringFormat(log4j);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		log4j.info("runLog4jTest(): Time=" + convert + "ms");
	}

	@Test
	public void runLog4jWithCheck4Test() {
		org.apache.log4j.Logger log4j = Logger.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (log4j.isDebugEnabled()) {
				log4jWithMessageFormat(log4j);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		log4j.info("runLog4jTest(): Time=" + convert + "ms");
	}


	/**
	 * ===========================================================================
	 * Logback logging with isDebugEnabled() Check
	 * ===========================================================================
	 */


	@Test
	public void runLogbackWithCheck1Test() {
		org.slf4j.Logger logback = LoggerFactory.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (logback.isDebugEnabled()) {
				logbackjNormalString(logback);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		logback.info("runLogbackTest(): Time=" + convert + "ms");
	}

	@Test
	public void runLogbackWithCheck2Test() {
		org.slf4j.Logger logback = LoggerFactory.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (logback.isDebugEnabled()) {
				logbackWithStringBuilder(logback);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		logback.info("runLogbackTest(): Time=" + convert + "ms");
	}

	@Test
	public void runLogbackWithCheck3Test() {
		org.slf4j.Logger logback = LoggerFactory.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (logback.isDebugEnabled()) {
				logbackWithStringFormat(logback);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		logback.info("runLogbackTest(): Time=" + convert + "ms");
	}

	@Test
	public void runLogbackWithCheck4Test() {
		org.slf4j.Logger logback = LoggerFactory.getLogger(LoggingBenchmarkTest2.class);
		stopWatch.start();
		for (int i = 0; i < 300000000; i++) {
			if (logback.isDebugEnabled()) {
				logbackWithMessageFormat(logback);
			}
		}
		stopWatch.stop();
		long convert = TimeUnit.MILLISECONDS.convert(stopWatch.getNanoTime(), TimeUnit.NANOSECONDS);
		logback.info("runLogbackTest(): Time=" + convert + "ms");
	}

}
